﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using V5_Model;
using System.Xml;
using System.Data;
using V5_Common;
using V5_DAL;

namespace V5_BLL
{
    public class BLLConfig
    {
        DALConfig dal = new DALConfig();

        public BLLConfig()
        {

        }

        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            return dal.Exists(ID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ModelConfig model)
        {
            dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(ModelConfig model)
        {
            dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ID)
        {

            dal.Delete(ID);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelConfig GetModel(int ID)
        {

            return dal.GetModel(ID);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中。
        /// </summary>
        public ModelConfig GetModelByCache(int ID)
        {

            string CacheKey = "ModelConfigModel-" + ID;
            object objModel = DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(ID);
                    if (objModel != null)
                    {
                        int ModelCache = ConfigHelper.GetConfigInt("ModelCache");
                        DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (ModelConfig)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<ModelConfig> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<ModelConfig> DataTableToList(DataTable dt)
        {
            List<ModelConfig> modelList = new List<ModelConfig>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                ModelConfig model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new ModelConfig();
                    if (dt.Rows[n]["ID"].ToString() != "")
                    {
                        model.ID = int.Parse(dt.Rows[n]["ID"].ToString());
                    }
                    model.SiteTitle = dt.Rows[n]["SiteTitle"].ToString();
                    model.SiteUrl = dt.Rows[n]["SiteUrl"].ToString();
                    model.SiteLogo = dt.Rows[n]["SiteLogo"].ToString();
                    model.WebmasterName = dt.Rows[n]["WebmasterName"].ToString();
                    model.WebmasterEmail = dt.Rows[n]["WebmasterEmail"].ToString();
                    model.SiteCopyright = dt.Rows[n]["SiteCopyright"].ToString();
                    model.SiteDescription = dt.Rows[n]["SiteDescription"].ToString();
                    model.SiteKeywords = dt.Rows[n]["SiteKeywords"].ToString();
                    if (dt.Rows[n]["SiteState"].ToString() != "")
                    {
                        model.SiteState = int.Parse(dt.Rows[n]["SiteState"].ToString());
                    }
                    model.SiteCloseText = dt.Rows[n]["SiteCloseText"].ToString();
                    if (dt.Rows[n]["MailObject"].ToString() != "")
                    {
                        model.MailObject = int.Parse(dt.Rows[n]["MailObject"].ToString());
                    }
                    model.MailServer = dt.Rows[n]["MailServer"].ToString();
                    model.MailServerUserName = dt.Rows[n]["MailServerUserName"].ToString();
                    model.MailServerPassWord = dt.Rows[n]["MailServerPassWord"].ToString();
                    model.MailDomain = dt.Rows[n]["MailDomain"].ToString();
                    model.TemplatePath = dt.Rows[n]["TemplatePath"].ToString();
                    model.UrlRewrite = dt.Rows[n]["UrlRewrite"].ToString();
                    model.UploadType = dt.Rows[n]["UploadType"].ToString();
                    model.UploadMwidth = dt.Rows[n]["UploadMwidth"].ToString();
                    model.UploadSwidth = dt.Rows[n]["UploadSwidth"].ToString();
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere)
        {
            return dal.GetList(PageSize, PageIndex, strWhere);
        }

        #endregion  成员方法

        public ModelConfig GetModel()
        {
            ModelConfig model = dal.GetModel(0);
            //string keyName;
            //XmlDocument xmldoc = new XmlDocument();
            //try
            //{
            //    xmldoc.Load(_filePath);
            //}
            //catch
            //{
            //    throw new Exception("读取有误!请检查" + _filePath + "是否存在!");
            //}
            //XmlNodeList DocdNodeName = xmldoc.DocumentElement.ChildNodes;
            //foreach (XmlNode node in DocdNodeName)
            //{
            //    keyName = node.Attributes["key"].InnerXml.ToString().Trim().ToLower();
            //    switch (keyName)
            //    {
            //        case "templatepath":
            //            model.TemplatePath = node.Attributes["value"].Value;
            //            break;
            //    }
            //}
            return model;
        }

        //public void UpdateAppSettings(ModelConfig model)
        //{
        //    string keyName;
        //    XmlDocument xmldoc = new XmlDocument();
        //    try
        //    {
        //        xmldoc.Load(_filePath);
        //    }
        //    catch
        //    {
        //        throw new Exception("读取有误!请检查" + _filePath + "是否存在!");
        //    }
        //    XmlNodeList DocdNodeName = xmldoc.DocumentElement.ChildNodes;
        //    foreach (XmlNode node in DocdNodeName)
        //    {
        //        keyName = node.Attributes["key"].InnerXml.ToString().Trim().ToLower();
        //        switch (keyName)
        //        {
        //            case "templatepath":
        //                node.Attributes["value"].Value = model.TemplatePath;
        //                break;
        //        }
        //    }
        //    try
        //    {
        //        xmldoc.Save(_filePath);
        //    }
        //    catch
        //    {
        //        throw new Exception("修改有误!请检查" + _filePath + "是否存在!");
        //    }
        //}

    }
}
